import com.google.gson.Gson;
import model.*;
import unit.HttpTurnClass;
import unit.RemoteInterFaceClass;
import unit.SqliteClass;
import unit.ToLocalClass;

import java.io.File;
import java.io.IOException;
import java.sql.*;
import java.util.Date;
import java.util.List;

public class UpdateData {
    public static void main(String[] args) {
        final String sn = "sn123456";
        final String mac = "EO-D5-5E-96-89-3B";
        DoClass doClass = new DoClass();
        DownLoad downLoad = new DownLoad(doClass, sn, mac);

        Thread threadDownLoad = new Thread(downLoad);
        threadDownLoad.start();
    }

    public static class DoClass implements DownLoad.doInterFace {
        /**
         * 是否正在运行
         */
        private boolean doSign;
        /**
         * 进度数
         */
        private byte progress;
        /**
         * 进度消息
         */
        private String message;
        /**
         * 总任务数量
         */
        private byte taskCount;

        public boolean isDoSign() {
            return doSign;
        }

        public byte getProgress() {
            return progress;
        }

        public String getMessage() {
            return message;
        }

        public byte getTaskCount() {
            return taskCount;
        }

        public DoClass() {
            this.doSign = false;
            this.progress = 0;
            message = null;
            taskCount = 0;
        }

        public void getDoStatus(byte progress, String message) {
            this.progress = progress;
            this.message = message;
            System.out.println(String.format("%d -- %d : %s", this.taskCount, this.progress, this.message));
        }

        public void beginEvent(byte taskCount, String message) {
            this.doSign = true;
            this.taskCount = taskCount;
            this.message = message;
            System.out.println(String.format("%d -- %d : %s", this.taskCount, this.progress, this.message));
        }

        public void endEvent(byte progress, String message) {
            this.doSign = false;
            this.progress = progress;
            this.message = message;
            System.out.println(String.format("%d -- %d : %s", this.taskCount, this.progress, this.message));
        }
    }

    /**
     * 门禁机参数下载
     */
    static class DownLoad implements Runnable {
        private doInterFace doInterFaceEvent;
        private final byte taskCount = 7;
        private byte progress;
        private String sn;
        private String mac;

        DownLoad(doInterFace doInterFaceEvent, String sn, String mac) {
            this.doInterFaceEvent = doInterFaceEvent;
            this.sn = sn;
            this.mac = mac;
            this.progress = 1;
        }

        public void run() {
            doInterFaceEvent.beginEvent(taskCount, "准备开始...");

            HttpTurnClass httpTurnClass = new HttpTurnClass();
            Gson gson = new Gson();
            //卫立星：要把梁昊给你的DoorLocalDB.db文件，放到D盘根目录；
            //或者，在此构造函数内，传入你实际的目录及文件名，否则，会报错;
            String db = "D:"
                    + File.separator
                    + "DoorLocalDB.db";
            SqliteClass sqliteClass = null;
            try {
                sqliteClass = new SqliteClass(db);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            String url_address_front = null;
            try {
                url_address_front = sqliteClass.getURL_ADDRESS_FRONT();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (url_address_front == null)
                url_address_front = "http://59.174.91.90:2000/api-a-akf-/";
            String url_address_log = null;
            try {
                url_address_log = sqliteClass.getURL_ADDRESS_LOG();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            if (url_address_log == null)
                url_address_log = "http://59.174.91.90:2000/api-a-okf-/";

            RemoteInterFaceClass remoteInterFaceClass = new RemoteInterFaceClass(
                    httpTurnClass.toOldChar(url_address_front)
                    , httpTurnClass.toOldChar(url_address_log));
            ToLocalClass toLocalClass = new ToLocalClass(
                    remoteInterFaceClass
                    , sqliteClass
                    , sn, mac);
//        下载参数
            ResultVO resultVO = null;
            try {
                resultVO = gson.fromJson(toLocalClass.getDoorParamList(), ResultVO.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (resultVO.getCode() == 0) {
                final List<EquipParaModel> list = gson.fromJson(resultVO.toString(), List.class);
                doInterFaceEvent.getDoStatus(progress, String.format("卫立星：远程接口获取%s条数据", list.size()));
                int count = 0;
                try {
                    toLocalClass.DeleteDoorParam(true);//清空new表的记录
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                for (Object row :
                        list) {
                    final EquipParaModel equipParaModel = gson.fromJson(row.toString(), EquipParaModel.class);
                    try {
                        count += toLocalClass.insertDoorPara(equipParaModel);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                list.clear();
                if (count > 0) {
                    try {
                        count = toLocalClass.insertDoorOldPara();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    doInterFaceEvent.getDoStatus(progress, String.format("卫立星：已下载%s条参数到门禁机本地，OK！", count));
                }
            }
            doInterFaceEvent.getDoStatus(progress++, "下载参数完成...");

            //下载消息与通知
            ResultVO resultVOMessage = null;
            try {
                resultVOMessage = gson.fromJson(toLocalClass.getDoorMessageList(), ResultVO.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (resultVOMessage.getCode() == 0) {
                final List<EquipmentMessageModel> list = gson.fromJson(resultVOMessage.toString(), List.class);
                doInterFaceEvent.getDoStatus(progress, String.format("卫立星：远程接口获取%s条消息与通知数据", list.size()));
                int count = 0;
                try {
                    toLocalClass.DeleteDoorMessage(true);//清空new表的记录
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                for (Object row :
                        list) {
                    final EquipmentMessageModel EquipmentMessageModel = gson.fromJson(row.toString(), EquipmentMessageModel.class);
                    try {
                        count += toLocalClass.insertDoorMessage(EquipmentMessageModel);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                list.clear();
                if (count > 0) {
                    try {
                        count = toLocalClass.insertDoorOldMessage();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    doInterFaceEvent.getDoStatus(progress, String.format("卫立星：已下载%s条消息与通知到门禁机本地，OK！", count));
                }
            }
            doInterFaceEvent.getDoStatus(progress++, "下载消息与通知完成...");

            //下载广告
            ResultVO resultVOAdv = null;
            try {
                resultVOAdv = gson.fromJson(toLocalClass.getDoorAdvList(), ResultVO.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (resultVOAdv.getCode() == 0) {
                final List<EquipmentAdvertisingModel> list = gson.fromJson(resultVOAdv.toString(), List.class);
                doInterFaceEvent.getDoStatus(progress, String.format("卫立星：远程接口获取%s条广告", list.size()));
                int count = 0;
                try {
                    toLocalClass.DeleteDoorAdv(true);//清空new表的记录
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                for (Object row :
                        list) {
                    final EquipmentAdvertisingModel equipmentAdvertisingModel = gson.fromJson(row.toString(), EquipmentAdvertisingModel.class);
                    try {
                        count += toLocalClass.insertDoorAdv(equipmentAdvertisingModel);
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
                list.clear();
                if (count > 0) {
                    try {
                        count = toLocalClass.insertDoorOldAdv();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                    doInterFaceEvent.getDoStatus(progress, String.format("卫立星：已下载%s条广告到门禁机本地，OK！", count));
                }
            }
            doInterFaceEvent.getDoStatus(progress++, "下载广告完成...");

//        下载RFID卡
            Date date = new Date();
            int cardCount = 0;
            int page_size = 10;
            try {
                page_size = sqliteClass.getPAGE_SIZE();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            int page = 1;
            ResultVOPage resultVOPage = null;
            try {
                resultVOPage = gson.fromJson(toLocalClass.getDoorCardList(page, page_size), ResultVOPage.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (resultVOPage.getCode() == 0) {
                int pageCount = (int) (resultVOPage.getCount() / page_size);
                if (resultVOPage.getCount() % page_size > 0) {
                    pageCount++;
                }
                try {
                    toLocalClass.DeleteDoorCardParam(true);//清空new表
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                final List list = gson.fromJson(resultVOPage.toString(), List.class);
                if (list != null && !list.isEmpty()) {
                    //存入第一页
                    for (Object row :
                            list) {
                        final DoorCardModel doorCardModel = gson.fromJson(row.toString(), DoorCardModel.class);
                        if (doorCardModel != null) {
                            try {
                                cardCount += toLocalClass.insertDoorCardPara(doorCardModel);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    //存入后续页
                    if (pageCount > 1) {
                        for (page = 2; page < pageCount + 1; page++) {
                            ResultVOPage resultVOPageNew = null;
                            try {
                                resultVOPageNew = gson.fromJson(toLocalClass.getDoorCardList(page, page_size), ResultVOPage.class);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if (resultVOPageNew.getCode() == 0) {
                                List listNew = gson.fromJson(resultVOPageNew.toString(), List.class);
                                if (listNew != null && !listNew.isEmpty()) {
                                    for (Object row :
                                            listNew) {
                                        final DoorCardModel doorCardModel = gson.fromJson(row.toString(), DoorCardModel.class);
                                        if (doorCardModel != null) {
                                            try {
                                                cardCount += toLocalClass.insertDoorCardPara(doorCardModel);
                                            } catch (SQLException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                    doInterFaceEvent.getDoStatus(progress, String.format("下载RFID，更新第%d页，每页%d条。", page, page_size));
                                }
                            }
                        }
                    }
                    doInterFaceEvent.getDoStatus(progress, String.format("下载RFID数量%d，下载用时：%dms。"
                            , cardCount
                            , (new Date()).getTime() - date.getTime()));
                    if (cardCount > 0) {
                        date = new Date();
                        try {
                            toLocalClass.insertDoorCardOldPara();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        doInterFaceEvent.getDoStatus(progress, String.format("卫立星：已下载%s条RFID数据到门禁机本地，处理用时：%dms，OK！"
                                , cardCount
                                , (new Date()).getTime() - date.getTime()));
                    }
                }
                list.clear();
            }
            doInterFaceEvent.getDoStatus(progress++, "下载RFID完成...");

//        下载人脸
            date = new Date();
            page = 1;
            cardCount = 0;
            ResultVOPage resultVOPageFace = null;
            try {
                resultVOPageFace = gson.fromJson(toLocalClass.getDoorFaceList(page, page_size), ResultVOPage.class);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (resultVOPageFace.getCode() == 0) {
                int pageCount = (int) (resultVOPageFace.getCount() / page_size);
                if (resultVOPageFace.getCount() % page_size > 0) {
                    pageCount++;
                }
                try {
                    toLocalClass.DeleteDoorFaceParam(true);//清空new表
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                final List list = gson.fromJson(resultVOPageFace.toString(), List.class);
                if (list != null && !list.isEmpty()) {
                    //存入第一页
                    for (Object row :
                            list) {
                        final DoorPictureModel doorPictureModel = gson.fromJson(row.toString(), DoorPictureModel.class);
                        if (doorPictureModel != null) {
                            try {
                                cardCount += toLocalClass.insertDoorFacePara(doorPictureModel);
                            } catch (SQLException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    //存入后续页
                    if (pageCount > 1) {
                        for (page = 2; page < pageCount + 1; page++) {
                            ResultVOPage resultVOPageNew = null;
                            try {
                                resultVOPageNew = gson.fromJson(toLocalClass.getDoorFaceList(page, page_size), ResultVOPage.class);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            if (resultVOPageNew.getCode() == 0) {
                                List listNew = gson.fromJson(resultVOPageNew.toString(), List.class);
                                if (listNew != null && !listNew.isEmpty()) {
                                    for (Object row :
                                            listNew) {
                                        final DoorPictureModel doorPictureModel = gson.fromJson(row.toString(), DoorPictureModel.class);
                                        if (doorPictureModel != null) {
                                            try {
                                                cardCount += toLocalClass.insertDoorFacePara(doorPictureModel);
                                            } catch (SQLException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                    doInterFaceEvent.getDoStatus(progress, String.format("下载人脸，更新第%d页，每页%d条。", page, page_size));
                                }
                            }
                        }
                    }
                    doInterFaceEvent.getDoStatus(progress, String.format("下载人脸数量%d，下载用时：%dms。"
                            , cardCount
                            , (new Date()).getTime() - date.getTime()));
                    if (cardCount > 0) {
                        date = new Date();
                        try {
                            toLocalClass.insertDoorFaceOldPara();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        doInterFaceEvent.getDoStatus(progress, String.format("卫立星：已下载%s条人脸数据到门禁机本地，处理用时：%dms，OK！"
                                , cardCount
                                , (new Date()).getTime() - date.getTime()));

                    }
                }
                list.clear();
            }
            doInterFaceEvent.getDoStatus(progress++, "下载人脸数据完成...");
//            this.progress = 0;
            doInterFaceEvent.endEvent(progress, "OVER");
        }

        interface doInterFace {
            void getDoStatus(byte progress, String message);

            void beginEvent(byte taskCount, String message);

            void endEvent(byte progress, String message);
        }
    }
}
